package com.dnake.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dnake.Constant;
import com.dnake.entity.BaseHttpBean;
import com.dnake.entity.EventSettingBean;
import com.dnake.entity.UserBean;
import com.dnake.panel.R;
import com.dnake.utils.ToastUtil;
import com.orhanobut.logger.Logger;
import com.wx.android.common.util.SharedPreferencesUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * @author haoyuliu
 * @since 2017/7/18
 */

public class FaceScoreFragment extends SupportFragment {
    @Bind(R.id.tv_score)
    TextView tvScore;
    @Bind(R.id.sb_score)
    SeekBar sbScore;
    @Bind(R.id.rb_one)
    RadioButton rbOne;
    @Bind(R.id.rb_two)
    RadioButton rbTwo;
    @Bind(R.id.tv_papers_score)
    TextView tvPapersScore;
    @Bind(R.id.sb_papers_score)
    SeekBar sbPapersScore;
    @Bind(R.id.rb_three)
    RadioButton rbThree;
    @Bind(R.id.rb_four)
    RadioButton rbFour;
    private View mView;

    public static FaceScoreFragment newInstance() {
        return new FaceScoreFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_face_setting, container, false);
        ButterKnife.bind(this, mView);
        initData();
        return mView;
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    private void initData() {
        //锁状态 true开启 false关闭
        if (SharedPreferencesUtils.getBoolean(Constant.SP_FACE_LIVE)) {
            rbOne.setChecked(true);
        } else {
            rbTwo.setChecked(true);
        }
        //离线验证 0 关闭 1开启
        if (SharedPreferencesUtils.getInt(Constant.SP_PERMISSION)==0){
            rbFour.setChecked(true);
        }else {
            rbThree.setChecked(true);
        }
        sbPapersScore.setProgress(SharedPreferencesUtils.getInt(Constant.SP_FACE_PAPERS_SCORE));
        sbScore.setProgress(SharedPreferencesUtils.getInt(Constant.SP_FACE_SCORE));
        tvScore.setText(SharedPreferencesUtils.getInt(Constant.SP_FACE_SCORE) + "分");
        tvPapersScore.setText(SharedPreferencesUtils.getInt(Constant.SP_FACE_PAPERS_SCORE) + "分");
    }

    @Override
    public boolean onBackPressedSupport() {
        return super.onBackPressedSupport();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventSetting(EventSettingBean eventBusBean) {
        if (eventBusBean.getCode() == Constant.EVENTBUS_FACE_SET) {
            if ("1".equals(eventBusBean.getName())) {
                if (SharedPreferencesUtils.getBoolean(Constant.SP_FACE_LIVE)) {
                    rbTwo.setChecked(true);
                    SharedPreferencesUtils.put(Constant.SP_FACE_LIVE, false);
                } else {
                    rbOne.setChecked(true);
                    SharedPreferencesUtils.put(Constant.SP_FACE_LIVE, true);
                }
            } else if ("3".equals(eventBusBean.getName())){
                if (SharedPreferencesUtils.getInt(Constant.SP_PERMISSION)==0) {
                    rbThree.setChecked(true);
                    SharedPreferencesUtils.put(Constant.SP_PERMISSION, 1);
                } else {
                    rbFour.setChecked(true);
                    SharedPreferencesUtils.put(Constant.SP_PERMISSION, 0);
                }
            }else if ("up".equals(eventBusBean.getName())) {
                if (SharedPreferencesUtils.getInt(Constant.SP_FACE_SCORE) < 100) {
                    SharedPreferencesUtils.put(Constant.SP_FACE_SCORE, SharedPreferencesUtils.getInt(Constant.SP_FACE_SCORE) + 1);
                    sbScore.setProgress(SharedPreferencesUtils.getInt(Constant.SP_FACE_SCORE));
                    tvScore.setText(SharedPreferencesUtils.getInt(Constant.SP_FACE_SCORE) + "分");
                }
            } else if ("down".equals(eventBusBean.getName())) {
                if (SharedPreferencesUtils.getInt(Constant.SP_FACE_SCORE) > 50) {
                    SharedPreferencesUtils.put(Constant.SP_FACE_SCORE, SharedPreferencesUtils.getInt(Constant.SP_FACE_SCORE) - 1);
                    sbScore.setProgress(SharedPreferencesUtils.getInt(Constant.SP_FACE_SCORE));
                    tvScore.setText(SharedPreferencesUtils.getInt(Constant.SP_FACE_SCORE) + "分");
                }
            } else if ("2".equals(eventBusBean.getName())) {
                if (SharedPreferencesUtils.getInt(Constant.SP_FACE_PAPERS_SCORE) < 100) {
                    SharedPreferencesUtils.put(Constant.SP_FACE_PAPERS_SCORE, SharedPreferencesUtils.getInt(Constant.SP_FACE_PAPERS_SCORE) + 1);
                    sbPapersScore.setProgress(SharedPreferencesUtils.getInt(Constant.SP_FACE_PAPERS_SCORE));
                    tvPapersScore.setText(SharedPreferencesUtils.getInt(Constant.SP_FACE_PAPERS_SCORE) + "分");
                }
            } else if ("8".equals(eventBusBean.getName())) {
                if (SharedPreferencesUtils.getInt(Constant.SP_FACE_PAPERS_SCORE) > 30) {
                    SharedPreferencesUtils.put(Constant.SP_FACE_PAPERS_SCORE, SharedPreferencesUtils.getInt(Constant.SP_FACE_PAPERS_SCORE) - 1);
                    sbPapersScore.setProgress(SharedPreferencesUtils.getInt(Constant.SP_FACE_PAPERS_SCORE));
                    tvPapersScore.setText(SharedPreferencesUtils.getInt(Constant.SP_FACE_PAPERS_SCORE) + "分");
                }
            }else if ("4".equals(eventBusBean.getName())){
                getUserFaceInfo();
            }
        }
    }
    /**
     * 获取人脸信息               后台注释 没实际使用
     */
    private void getUserFaceInfo() {
        RequestParams params = new RequestParams(Constant.HTTP_URL + "/device/offline/info/" + SharedPreferencesUtils.getString(Constant.SP_LOCATION_ID));
        x.http().get(params, new Callback.CommonCallback<String>() {
            @SuppressLint("CheckResult")
            @Override
            public void onSuccess(String result) {
                Logger.e(result);
                if (!TextUtils.isEmpty(result)) {
                    BaseHttpBean httpBean = JSON.parseObject(result, BaseHttpBean.class);
                    if (!TextUtils.isEmpty(httpBean.getData())) {
                        ToastUtil.show("同步成功");
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }
    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
